package org.smartjava.concurrent.juc.aqs;

import org.smartjava.util.PrintUtil;
import org.smartjava.util.ThreadUtil;


import java.util.concurrent.CyclicBarrier;

import static org.smartjava.util.ThreadUtil.sleepRandom;
import static org.smartjava.util.ThreadUtil.threadName;
import static org.smartjava.util.TimeUtil.currentTime;
import static org.smartjava.util.PrintUtil.print;

public class CyclicBarrierTest {
    public static void main(String[] args) {
        int count = 5;
        CyclicBarrier cb = new CyclicBarrier(count);
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    sleepRandom(3);
                    print(currentTime(), threadName(), "到达集合地点");
                    cb.await();
                    print(currentTime(), threadName(), "出发去公园");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "同伴-" + i).start();
        }
    }
}
